package com.dcm.item.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.common.utils.IdWorker;
import com.dcm.item.entity.CommentEntity;
import com.dcm.item.mapper.CommentMapper;
import com.dcm.item.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: liutaoran
 * @date: 2021-03-25 21:09
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<CommentEntity> findAll() {

        return commentMapper.findAll();
    }

    @Override
    public CommentEntity findOne(Long id)  throws MyException {
        if (id==null) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        return commentMapper.findById(id);
    }

    @Override
    public void delete(Long id) throws MyException {
        if (StringUtils.isEmpty(id.toString())) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        commentMapper.deleteById(id);
    }

    @Override
    public void update(CommentEntity commentEntity)  {
        commentMapper.updateById(commentEntity);
    }



    @Override
    public void insert(CommentEntity commentEntity) {
        commentEntity.setId(new IdWorker().nextId());
        commentMapper.insert(commentEntity);
    }

    @Override
    public List<CommentEntity> findByUserName(String userName) {
        return commentMapper.findByUserName(userName);
    }

    @Override
    public List<CommentEntity> findByItemName(String itemName) {
        return commentMapper.findByItemName(itemName);
    }

    @Override
    public List<CommentEntity> findByStatus(String status) {
        return commentMapper.findByStatus(status);
    }

    @Override
    public List<CommentEntity> today() {
        return commentMapper.today();
    }

    @Override
    public List<CommentEntity> yesterday() {
        return commentMapper.yesterday();
    }

    @Override
    public List<CommentEntity> days() {
        return commentMapper.days();
    }

    @Override
    public List<CommentEntity> month() {
        return commentMapper.month();
    }

    @Override
    public List<CommentEntity> year() {
        return commentMapper.year();
    }

    @Override
    public PageInfo<CommentEntity> findPage(Integer currentPage, Integer pageSize) {
        if (currentPage == null || pageSize == null) {
            throw new MyException(Constants.ResponseMsg.PAGE_PARAMS_NOT_NULL);
        }

        PageHelper.startPage(currentPage, pageSize);
        List<CommentEntity> list = commentMapper.findAll();
        //用PageInfo对结果进行包装
        PageInfo<CommentEntity> page = new PageInfo<>(list);

        return page;
    }


}
